package com.kakarote.examine.service.impl;

import cn.hutool.core.collection.CollectionUtil;
import cn.hutool.core.util.ObjectUtil;
import com.kakarote.core.feign.admin.service.AdminService;
import com.kakarote.core.servlet.BaseServiceImpl;
import com.kakarote.examine.entity.PO.ExamineAdvancedConfigLimitTime;
import com.kakarote.examine.entity.PO.ExamineRecord;
import com.kakarote.examine.entity.PO.ExamineRecordLimit;
import com.kakarote.examine.mapper.ExamineRecordLimitMapper;
import com.kakarote.examine.service.IExamineAdvancedConfigLimitTimeService;
import com.kakarote.examine.service.IExamineRecordLimitService;
import com.kakarote.examine.service.IExamineRecordService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.time.LocalDateTime;
import java.util.ArrayList;
import java.util.List;
import java.util.stream.Collectors;

/**
 * <p>
 * 超时转交人 服务实现类
 * </p>
 *
 * @author zyl
 * @since 2022-09-05
 */
@Service
public class ExamineRecordLimitServiceImpl extends BaseServiceImpl<ExamineRecordLimitMapper, ExamineRecordLimit> implements IExamineRecordLimitService {

    @Autowired
    private IExamineAdvancedConfigLimitTimeService advancedConfigLimitTimeService;

    @Autowired
    private IExamineRecordService iExamineRecordService;
    @Autowired
    private AdminService adminService;

    @Override
    public void addOrUpdateRecordLimit(Long recordId) {
        ExamineRecord byId = iExamineRecordService.getById(recordId);
        if (ObjectUtil.isEmpty(byId)) {
            return;
        }
        ExamineAdvancedConfigLimitTime examineAdvancedConfigLimitTime = advancedConfigLimitTimeService.queryConfigByRecordId(recordId);
        if (ObjectUtil.isEmpty(examineAdvancedConfigLimitTime)) {
            return;
        }
        //计算时间
        Double limitTime = examineAdvancedConfigLimitTime.getLimitTime();
        String limitTimeUnit = examineAdvancedConfigLimitTime.getLimitTimeUnit();
        long minutes = 0L;
        switch (limitTimeUnit) {
            case "minute":
                Double minute = limitTime * 60;
                minutes = minute.longValue();
                break;
            case "hour":
                Double hours = limitTime * 3600;
                minutes = hours.longValue();
                break;
            case "day":
                Double days = limitTime * 3600 * 24;
                minutes = days.longValue();
                break;
            default:
                return;
        }
        LocalDateTime nowTime = LocalDateTime.now();
        nowTime = nowTime.plusSeconds(minutes);
        //插入or更新
        List<ExamineRecordLimit> list = lambdaQuery().eq(ExamineRecordLimit::getRecordId, recordId).list();
        if (CollectionUtil.isNotEmpty(list)) {
            lambdaUpdate().set(ExamineRecordLimit::getNoticeTime, nowTime).eq(ExamineRecordLimit::getRecordId, recordId).update();
        } else {
            ExamineRecordLimit examineRecordLimit = new ExamineRecordLimit();
            examineRecordLimit.setRecordId(recordId);
            examineRecordLimit.setNoticeTime(nowTime);
            saveOrUpdate(examineRecordLimit);
        }
    }

    @Override
    public void removeRecordLimit(Long recordId) {
        removeById(recordId);
    }

    @Override
    public List<Long> queryRecordList() {
        List<ExamineRecordLimit> examineRecordLimit = lambdaQuery().le(ExamineRecordLimit::getNoticeTime, LocalDateTime.now()).list();
        if (CollectionUtil.isNotEmpty(examineRecordLimit)) {
            return examineRecordLimit.stream().map(ExamineRecordLimit::getRecordId).collect(Collectors.toList());
        }
        return new ArrayList<>();
    }




    public LocalDateTime getNoticeTime(Double limitTime, String limitTimeUnit, LocalDateTime sourceTime) {
        if (ObjectUtil.isEmpty(limitTime) || ObjectUtil.isEmpty(limitTimeUnit)) {
            return sourceTime;
        }
        //计算时间
        long minutes = 0L;
        switch (limitTimeUnit) {
            case "minute":
                Double minute = limitTime * 60;
                minutes = minute.longValue();
                break;
            case "hour":
                Double hours = limitTime * 3600;
                minutes = hours.longValue();
                break;
            case "day":
                Double days = limitTime * 3600 * 24;
                minutes = days.longValue();
                break;
        }
        sourceTime = sourceTime.plusSeconds(minutes);
        return sourceTime;
    }
}
